let recipes//记录菜单
//个人信息
$.ajax({
    url: '../../UserData',
    type: "POST",
    data: {
        UserID: $("#user_id").attr("data-userid")
    }, success: function (data) {

        $("#head_data").children().eq(0).children().text(data.result.UserName)
        $("#head_data").children().eq(1).children().text(data.result.UserID)
    }
})

let recipe_mode = function (recipes) {
    $("#content").html('')
    $.each(recipes, function (index, value) {
        $("#content").html($("#content").html() + `<div class="col-md-6">
    <div class="row g-0 border rounded overflow-hidden flex-md-row mb-4 shadow-sm h-md-250 position-relative">
        <div class="col-auto  d-lg-block">
            <img class="bd-placeholder-img" id="Food_img" width="200" height="250"
                src="../../${value.Samplefigure}" role="img" aria-label="Placeholder: 6"
                preserveAspectRatio="xMidYMid slice" focusable="false" />
        </div>
        <div class="col p-4 d-flex flex-column position-static"  style="background-color: #dfe4ea;">
            <strong class="d-inline-block mb-5 h1" style="color:#2f3542;">菜名：${value.FoodName}
                <input name="check" type="checkbox" style="display:none" class="checks" data-foodid="${value.FoodID}"></strong>
            <div class="mb-1 text-muted mb-3 h4">发布时间：${value.Release_time}</div>
            <div class="mb-1 text-muted mb-3 h3">收 藏 量：${value.CollectionNum}</div>
            <p class="mt-3"><b>食材:</b>${value.Formula}</p>
        </div>
    </div>
</div>`)
    })
}

//菜品信息
$.ajax({
    url: '../../user_recipe',
    type: "POST",
    data: {
        UserID: $("#user_id").attr("data-userid")
    }, success: function (data) {
        if (data.code == 0) {
            recipes = data.data
            recipe_mode(recipes)
        }

    }
})

//排序
$('#sort').children().on('click', function () {
    let old_recipes = recipes
    console.log($(this).children());
    if ($(this).children().text() == '发布时间') {
        old_recipes = old_recipes.sort(function (a, b) { return new Date(a.Release_time) > new Date(b.Release_time) })//按时间排序
        // var formatDate1 = new Date(recipes[0].Release_time)
        recipe_mode(old_recipes);
    } else if ($(this).children().text() == '收藏人数') {
        old_recipes = old_recipes.sort(function (a, b) { return a.CollectionNum - b.CollectionNum })//按时间排序
        recipe_mode(old_recipes);
    } else recipe_mode(recipes);
})

//搜索
$('#search').on('input', function () {
    let old_recipes = []
    $.each(recipes, function (index, value) {
        if (value.FoodName.indexOf($('#search').val()) == 0) {
            old_recipes[old_recipes.length] = value
        }
    })
    if ($('#search').val() == '')
        recipe_mode(recipes)
    else recipe_mode(old_recipes)
})

//删除按键显示
$("#choose").on('click',function(){
    if($(this).prev().attr('class')=='nav-link disabled'){
        $(this).prev().attr('class','nav-link')
        $(this).prev().css('color','white')
        $('.checks').css('display','inline')
    }else {
        $(this).prev().attr('class','nav-link disabled')
        $(this).prev().css('color','#212529')
        $('.checks').css('display','none')
    }
})

//提交删除
$('#_delete_').on('click',function(){
    let key=true
    $.each($('.checks'),function(index,value){
        if($(value).prop('checked'))
        $.ajax({
            url:'../../admin_delete',
            type:"POST",
            data:{
                FoodID:$(value).attr('data-foodid')
            },
            success:function(data){
                if(data.code==0){
                console.log(data);
            }else{
                    alert($(value).attr('data-foodid')+'删除失败')
                    key=false
                }
            }
        })
    })
    if(key){
        alert('全部删除成功')
    }
    window.location.href='./'+$("#user_id").attr('data-userid')
})
